package org.corey.share.db.service;

import java.util.Calendar;
import java.util.Date;

import org.corey.share.db.dao.ArticleDao;
import org.corey.share.db.dao.BaseDao;
import org.corey.share.db.entity.Article;
import org.corey.share.db.entity.ArticleCatogory;
import org.corey.share.db.manager.DefaultDaoFactory;
import org.corey.share.db.manager.EM;
import org.corey.share.misc.Constants;
import org.corey.share.misc.PageData;

public class ArticleServiceImpl implements ArticleService {
	public ArticleDao dao = new DefaultDaoFactory().createArticleDao();

	public boolean writeArticle(Article article) {
		if (article == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean success = dao.saveArticle(article);
		EM.commitTransaction();
		return success;
	}

	public boolean deleteArticle(Long id) {
		if (id == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean success = dao.removeArticle(id);
		EM.commitTransaction();
		return success;
	}

	private PageData<Article> loadAllArticles(int pageSize, int pageIndex) {
		if (pageSize < 0 || pageIndex < 0) {
			throw new IllegalArgumentException();
		}
		PageData<Article> data = dao.loadAllArticles(pageSize, pageIndex,
				"writeDate desc");
		for (Article a : data.getPageData()) {
			Long catogory = a.getCatogory();
			if (catogory != null) {
				ArticleCatogory belongCatogory = (ArticleCatogory) ((BaseDao) dao)
						.load(catogory, ArticleCatogory.class);
				if (belongCatogory != null) {
					a.setCatogoryName(belongCatogory.getName());
				}

			}
		}
		return data;
	}

	public Article loadArticle(Long id) {
		if (id == null) {
			throw new IllegalArgumentException();
		}
		Article article = dao.loadArticle(id);
		Long catogory = article.getCatogory();
		if (catogory != null) {
			ArticleCatogory belongCatogory = (ArticleCatogory) ((BaseDao) dao)
					.load(catogory, ArticleCatogory.class);
			if (belongCatogory != null) {
				article.setCatogoryName(belongCatogory.getName());
			}

		}
		return article;
	}

	public PageData<Article> loadArticlesInDate(int pageSize, int pageIndex,
			int year, int month) {
		if (year < 1900 || year > 3000 || pageSize < 0 || pageIndex < 0
				|| month < 1 || month > 12) {
			throw new IllegalArgumentException();
		}
		int endYear = 0;
		int endMonth = 0;
		int day = 1;
		if (month == 12) {
			endYear = year + 1;
			endMonth = 1;
		} else {
			endYear = year;
			endMonth = month + 1;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		Date startDate = calendar.getTime();
		calendar.clear();
		calendar.set(endYear, endMonth, day);
		Date endDate = calendar.getTime();
		PageData<Article> data = dao.loadArticlesWroteBetween(pageSize,
				pageIndex, startDate, endDate);
		for (Article a : data.getPageData()) {
			Long catogory = a.getCatogory();
			if (catogory != null) {
				ArticleCatogory belongCatogory = (ArticleCatogory) ((BaseDao) dao)
						.load(catogory, ArticleCatogory.class);
				if (belongCatogory != null) {
					a.setCatogoryName(belongCatogory.getName());
				}

			}
		}
		return data;
	}

	public boolean modifyArticle(Article article) {
		if (article == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean success = dao.updateArticle(article);
		EM.commitTransaction();
		return success;
	}

	public boolean deleteArticles(Long[] ids) {
		if (ids == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		for (Long id : ids) {
			boolean success = dao.removeArticle(id);
			if (!success) {
				return success;
			}
		}
		EM.commitTransaction();
		return true;
	}

	public PageData<Article> loadAllArticles(int pageSize,
			int pageIndex, Long catogory) {
		if (pageSize < 0 || pageIndex < 0) {
			throw new IllegalArgumentException();
		}
		if(catogory==Constants.ANY_ARTICLE_CATOGORY){
			return this.loadAllArticles(pageSize, pageIndex);
		}
		PageData<Article> data = dao.loadArticlesbyCatogory(pageSize, pageIndex,
				"writeDate desc",catogory);
		for (Article a : data.getPageData()) {
			if (catogory != null) {
				ArticleCatogory belongCatogory = (ArticleCatogory) ((BaseDao) dao)
						.load(catogory, ArticleCatogory.class);
				if (belongCatogory != null) {
					a.setCatogoryName(belongCatogory.getName());
				}

			}
		}
		return data;
	}
	
	

}
